package com.dc.toolkit.hutool.core;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.CharsetUtil;
import com.google.common.collect.Lists;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.junit.Test;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;

/**
 * Description: Hutool Convert
 * @author: gdc
 * @date: 2021/5/8
 * @version 1.0
 */
public class ConvertTest {

    /**
     * Convert.toStr()
     * 转换为字符串
     */
    @Test
    public void test_toStr() {
        int a = 1;
        String str1 = Convert.toStr(a);
        System.out.println(str1);

        String str2 = Convert.toStr(null);
        System.out.println("null转换，没有默认值：" + str2);

        String str3 = Convert.toStr(null, "空");
        System.out.println("null转换，有默认值：" + str3);

        long[] b = {1,2,3,4,5};
        String str4 = Convert.toStr(b);
        System.out.println(str4);
    }

    /**
     * Convert.toLong()
     * 转换为字符串
     */
    @Test
    public void test_toLong() {
        Long l1 = Convert.toLong("10");
        System.out.println("数据转换为Long:" + l1);

        Long l2 = Convert.toLong("11sss");
        System.out.println("数据转换为Long, 开始为数字，后面夹带其他字符【数字会转换】:" + l2);

        Long l3 = Convert.toLong("sss12");
        System.out.println("数据转换为Long，开始为字符串，后面为数字【数字不会转换】:" + l3);

        Long l4 = Convert.toLong(null);
        System.out.println("null转换，没有默认值：" + l4);

        Long l5 = Convert.toLong(null, 100L);
        System.out.println("null转换，有默认值：" + l5);
    }

    /**
     * 转换为指定类型数组
     * Convert.toIntArray()
     */
    @Test
    public void test_toIntArray() {
        String[] strs = {"1","2","3","4"};
        Integer[] ints = Convert.toIntArray(strs);
        Stream.of(ints).forEach(System.out::println);

        System.out.println("--------------------------");

        long[] longs = {5,6,7,8};
        Integer[] ints2 = Convert.toIntArray(longs);
        Stream.of(ints2).forEach(System.out::println);
    }

    /**
     * 转换为BigDecimal
     * Convert.toBigDecimal()
     */
    @Test
    public void test_toBigDecimal() {
        BigDecimal decimal1 = Convert.toBigDecimal("12.345");
        System.out.println(decimal1);

        BigDecimal decimal2 = Convert.toBigDecimal("2.345sdfs");
        System.out.println(decimal2);

        BigDecimal decimal3 = Convert.toBigDecimal("aaa2.345sdfs");
        System.out.println(decimal3);

        BigDecimal decimal4 = Convert.toBigDecimal(null);
        System.out.println(decimal4);

        BigDecimal decimal5 = Convert.toBigDecimal(null, BigDecimal.ZERO);
        System.out.println(decimal5);
    }

    /**
     * Convert.toDate()
     * 装化为日期
     */
    @Test
    public void test_toDate() {
        Date date = Convert.toDate("2018-10-01 ");
        System.out.println(date);

        Date date2 = Convert.toDate("");
        System.out.println(date2);
    }

    /**
     * Convert.convert(Type,object)
     * Convert.toList(object);
     * 装化为集合
     */
    @Test
    public void test_convert() {
        Object[] a = {"a","b","c",1,2,3};
        Convert.convert(List.class, a).forEach(System.out::println);

        List<?> list2 = Convert.toList(a);
        list2.stream().forEach(s -> System.out.print(s+"\t"));
    }

    /**
     * Convert.toHex()
     * 将字符串转换为 16进制
     * Convert.hexToStr()
     * 将 16进制转换为普通字符串
     * Convert.hexToBytes()
     * 16进制转为byte[]
     */
    @Test
    public void test_hex() {
        String str = "今天阳光明媚";
        String hex = Convert.toHex(str, CharsetUtil.CHARSET_UTF_8);
        System.out.println(hex);
        String new_str = Convert.hexToStr(hex, CharsetUtil.CHARSET_UTF_8);
        System.out.println(new_str);
        byte[] bytes = Convert.hexToBytes(hex);
    }

    /**
     * Unicode和字符串转换
     * Convert.strToUnicode()
     * Convert.unicodeToStr()
     */
    @Test
    public void test_unicode() {
        String str = "搬入新办公大楼";
        String unicodeStr = Convert.strToUnicode(str);
        System.out.println(unicodeStr);
        String newStr = Convert.unicodeToStr(unicodeStr);
        System.out.println(newStr);
    }

    /**
     * 字符编码转换
     * Convert.convertCharset()
     */
    @Test
    public void test_Charset() {
        String str = "字符编码转换";
        String convertChar = Convert.convertCharset(str, CharsetUtil.UTF_8, CharsetUtil.ISO_8859_1);
        System.out.println(convertChar);
        String newStr = Convert.convertCharset(convertChar, CharsetUtil.ISO_8859_1, CharsetUtil.UTF_8);
        System.out.println(newStr);
    }

    /**
     * 时间单位转换
     * Convert.convertTime()
     */
    @Test
    public void test_convertTime() {
        long t = 1111111111;
        long days = Convert.convertTime(t, TimeUnit.MINUTES, TimeUnit.DAYS);
        System.out.println(days);
    }

    /**
     * 金额大小写转换
     * Convert.digitToChinese()
     * 1234.56  --> 壹仟贰佰叁拾肆元伍角陆分
     */
    @Test
    public void test_digitToChinese() {
        double d = 1234.56;
        String str = Convert.digitToChinese(d);
        System.out.println(str);
    }

    /**
     * 列表数据转换
     * Convert.toList()
     */
    @Test
    public void test_toList(){
        List<Cat> catList = Lists.newArrayList();
        for (int i = 0; i < 10; i++) {
            Cat cat = new Cat().setId(i).setName("姓名" + i).setAge(10 + i).setIsDel(i % 2 == 0);
            catList.add(cat);
        }

        catList.forEach(System.out::println);
        System.out.println("-----------------------------------------------");

        List<CatDTO> catDTOList = Convert.toList(CatDTO.class, catList);
        catDTOList.forEach(System.out::println);
    }

    /**
     * 对象转map
     * Convert.toMap()
     */
    @Test
    public void test_toMap(){
        Cat cat = new Cat().setId(12).setName("姓名12").setAge(10).setIsDel(true);
        Map<String, String> map = Convert.toMap(String.class, String.class, cat);
        System.out.println(map);
    }
}



@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
class Cat {

    /**
     * id
     */
    private Integer id;
    /**
     * 名字
     */
    private String name;
    /**
     * 年龄
     */
    private Integer age;

    /**
     * 是否删除
     */
    private Boolean isDel;

}


@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
class CatDTO {

    /**
     * id
     */
    private Integer id;
    /**
     * 名字
     */
    private String name;
    /**
     * 年龄
     */
    private Integer age;

    /**
     * 是否删除
     */
    private Boolean isDel;

}